package com.lottery.ruok.lottrey.fragment;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;
import android.widget.ImageView;

import com.applozic.mobicomkit.uiwidgets.conversation.messageUtils.ChatRecordUtils;
import com.applozic.mobicomkit.uiwidgets.conversation.messageUtils.SearchRecordUtils;
import com.lottery.ruok.lottrey.R;
import com.lottery.ruok.lottrey.activity.ActivitiesCenterActivity;
import com.lottery.ruok.lottrey.adapter.ActivitiesAdapter;
import com.lottery.ruok.lottrey.eventbusObject.ActivitiesCenterActivityObj;
import com.lottery.ruok.lottrey.eventbusObject.ActivitiesCenterRefreshEvent;
import com.lottery.ruok.lottrey.eventbusObject.LoginSuccess;
import com.lottery.ruok.lottrey.ext.RecyclerViewExt;
import com.lottery.ruok.lottrey.retrofitModule.RetrofitBase;
import com.lottery.ruok.lottrey.retrofitModule.RxHelper;
import com.lottery.ruok.lottrey.retrofitModule.RxSubscribe;
import com.lottery.ruok.lottrey.retrofitModule.request.ActivityListRequest;
import com.lottery.ruok.lottrey.retrofitModule.response.ActivityListResponse;
import com.lottery.ruok.lottrey.userInfo.UserInfo;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.Unbinder;

/**
 * Created by ASUS on 2017/7/27.
 */

public class ChildActivitiesListFragment extends LazyFragment {

    @BindView(R.id.rv_activities_list)
    RecyclerViewExt rvActivitiesList;
    @BindView(R.id.refreshLayout)
    SmartRefreshLayout refreshLayout;
    Unbinder unbinder;
    @BindView(R.id.bg_img)
    ImageView bgImg;

    private ActivitiesAdapter myAdapter;
    private ArrayList<ActivityListResponse.ListBean> mData;
    private String mStatus;
    private int first = 1;
    private boolean firstLoad = true;
    //有参加活动
    private boolean isRefresh = false;


    @Override
    protected int setContentView() {
        return R.layout.activities_child_fragment;
    }

    @Override
    protected void lazyLoadData(Bundle bundle) {
        mStatus = bundle.getString("status");
        if (first == 1) {
            if (mStatus.equals("all")) {
                loadData(null);
            } else {
                loadData(mStatus);

            }
        }
        first++;
    }


    private void initData() {
        if (myAdapter == null) {
            rvActivitiesList.setLayoutManager(new LinearLayoutManager(getActivity()));

            myAdapter = new ActivitiesAdapter(getActivity(), mData, new OnRefreshListener() {
                @Override
                public void onRefresh() {
                    loadData(mStatus);
                }
            }, rvActivitiesList);
            rvActivitiesList.setAdapter(myAdapter);
        } else {
            myAdapter.setData(mData);
            myAdapter.notifyDataSetChanged();
        }
    }

    private void loadData(String status) {
        RetrofitBase.getRetrofit().create(ActivityListRequest.class)
                .getResult(UserInfo.getToken(), null, status)
                .compose(RxHelper.<ActivityListResponse>handleResult())
                .subscribe(new RxSubscribe<ActivityListResponse>(mActivity) {
                    @Override
                    protected void _onNext(ActivityListResponse activityListResponse) {
                        if (refreshLayout != null) {
                            refreshLayout.finishRefresh();
                        }

                        if (activityListResponse.getList().size() == 0) {
                            mData = null;
                            bgImg.setVisibility(View.GONE);
                        } else {
                            bgImg.setVisibility(View.VISIBLE);
                            mData = activityListResponse.getList();
                        }
                        initData();
                    }

                    @Override
                    protected boolean showDialog() {
                        return firstLoad;
                    }

                    @Override
                    public void onError(Throwable e) {
                        super.onError(e);
                        mData = null;
                        initData();
                        if (refreshLayout!=null) refreshLayout.finishRefresh();
                    }

                    @Override
                    public void onComplete() {
                        super.onComplete();
                        firstLoad = false;
                    }
                });


    }


    @Override
    protected void initView() {
        //参与活动后刷新
        refreshLayout.setOnRefreshListener(new com.scwang.smartrefresh.layout.listener.OnRefreshListener() {
            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                loadData(mStatus);
            }
        });
    }

    @Override
    public void onStop() {
        super.onStop();
        isRefresh = false;
    }

    @Override
    public void onResume() {
        super.onResume();
        if (isRefresh) {
            if (first!=1) loadData(mStatus);
        }
    }

    /**
     * 刷新活动List
     *
     * @param activitiesCenterRefreshEvent
     */

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMessageEvent(ActivitiesCenterRefreshEvent activitiesCenterRefreshEvent) {
        isRefresh = true;
    }

    /**
     * 登录
     *
     * @param event
     */
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMessageEvent(LoginSuccess event) {
        isRefresh = true;
    }


    public interface OnRefreshListener {
        void onRefresh();
    }
}